/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.ExecXML;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author HoangLiem
 */
public class AddTableModel extends AbstractTableModel {

    private ArrayList<String> columnNames;
    private ArrayList<ArrayList<Object>> data;
    private ArrayList<Object> longValues;

    public ArrayList<String> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(ArrayList<String> columnNames) {
        this.columnNames = columnNames;
    }

    public ArrayList<ArrayList<Object>> getData() {
        return data;
    }

    public void setData(ArrayList<ArrayList<Object>> data) {
        this.data = data;
    }

    public ArrayList<Object> getLongValues() {
        return longValues;
    }

    public void setLongValues(ArrayList<Object> longValues) {
        this.longValues = longValues;
    }

    public ArrayList<Object> initNewRow() {
        ArrayList<Object> row = new ArrayList<Object>();
        row.add(new String(""));
        row.add(new String(""));
        row.add(new Integer(10));
        row.add(new Boolean(false));
        row.add(new Boolean(false));
        row.add(new Boolean(false));
        return row;
    }

    public ArrayList<Object> initLongValues() {
        ArrayList<Object> longVal = new ArrayList<Object>();
        Object[] objs = new Object[]{"", "", 11, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE};
        for (int i = 0; i < objs.length; i++) {
            longVal.add(objs[i]);
        }
        return longVal;
    }

    public AddTableModel() {
        columnNames = new ArrayList<String>();
        columnNames.add("Field Name");
        columnNames.add("Data Type");
        columnNames.add("Length");
        columnNames.add("Primary Key");
        columnNames.add("Not Null");
        columnNames.add("Unique");
        data = new ArrayList<ArrayList<Object>>();
        for (int i = 0; i < 5; i++) {
            data.add(initNewRow());
        }
        longValues = initLongValues();
    }

    public AddTableModel(ArrayList<ArrayList<Object>> data) {
        columnNames = new ArrayList<String>();
        columnNames.add("Field Name");
        columnNames.add("Data Type");
        columnNames.add("Length");
        columnNames.add("Primary Key");
        columnNames.add("Not Null");
        columnNames.add("Unique");
        this.data = data;
        longValues = initLongValues();
    }

    public AddTableModel(ArrayList<String> columnNames, ArrayList<ArrayList<Object>> data, ArrayList<Object> longValues) {
        this.columnNames = columnNames;
        this.data = data;
        longValues = initLongValues();
        this.longValues = longValues;
    }

    @Override
    public int getColumnCount() {
        return columnNames.size();
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames.get(col);
    }

    @Override
    public Object getValueAt(int row, int col) {
        return data.get(row).get(col);
    }

    /*
     * Định nghĩa cách hiển thị lên table
     */
    @Override
    public Class getColumnClass(int c) {
        if (getValueAt(0, c) == null) {
            return String.class;
        }
        return getValueAt(0, c).getClass();
    }

    /*
     * Cho phép chỉnh sửa trên cell hay không
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        return true;
    }

    /*
     * set gia tri tai một cell trong table
     */
    public void setValue(Object value, int row, int col) {
        ArrayList<Object> oldRow = data.get(row);
        oldRow.add(col, value);
        oldRow.remove(col + 1);
        data.add(row, oldRow);
        data.remove(row + 1);
    }
    /*
     * Lấy tất cả field
     */

    public ArrayList<String> getAllFieldName() {
        ArrayList<String> fieldNameList = new ArrayList<String>();
        if (data != null && !data.isEmpty()) {
            for (int i = 0; i < data.size(); i++) {
                String fieldName = (String) data.get(i).get(0);
                if (!fieldName.equals("")) {
                    fieldNameList.add(fieldName);
                }
            }
        } else {
            fieldNameList = null;
        }
        return fieldNameList;
    }
    /*
     * Kiểm trà tên các trường trùng nhau
     */

    public boolean isDuplicatedFieldName() {
        boolean result = false;
        ArrayList<String> fieldNameList = getAllFieldName();
        if (fieldNameList != null && !fieldNameList.isEmpty()) {
            if (fieldNameList.size() > 1) {
                String first = "";
                String after = "";
                int length = fieldNameList.size();
                for (int i = 0; i < length - 1; i++) {
                    first = fieldNameList.get(i);
                    for (int j = i + 1; j < fieldNameList.size(); j++) {
                        after = fieldNameList.get(j);
                        if (first.equalsIgnoreCase(after)) {
                            return result = true;
                        }
                    }
                }
            }
        }
        return result;
    }
    /*
     * 
     */

    @Override
    public void setValueAt(Object value, int row, int col) {

        if (row == getRowCount() - 1) {
            data.add(initNewRow());
        }
        if (col == 2) {
            String type = (String) getValueAt(row, 1);
            if (type.equalsIgnoreCase("string")) {
                String str = value.toString();
                if (!ExecXML.isInteger(str)) {//da viet lai, ko vao toi đây
                    JOptionPane.showMessageDialog(null, "Value must be integer");
                    return;
                }
            }
        }
        if (col == 3) {
            boolean b = (Boolean) getValueAt(row, col);
            setValueAt(true, row, col + 1);
        }
        if (col == 4) {
            boolean b = (Boolean) getValueAt(row, 3);
            if (b == true) {
                return;
            }
        }
        if (col == 5) {
        }
        setValue(value, row, col);
        fireTableCellUpdated(row, col);
    }  
}
